package com.cheng.commonlibrary.utlis;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.text.TextUtils;

import com.cheng.commonlibrary.utlis.teim.TimeUtils;

import java.text.DecimalFormat;
import java.util.Calendar;

public final class ShowUtlis {
    /**
     * 将金额按照 三位一间隔，并且保留两位小数显示（eg : 12,340.10）
     */
    public static final String formatSum(String sum) {
        return formatSum(sum, 0);
    }

    /**
     * 将金额按照 三位一间隔，并且保留指定几位小数的显示
     */
    public static final String formatSum(String sum, int retain) {
        if (TextUtils.isEmpty(sum)) {
            sum = "0";
        }
        String pattern = "###,##0.";
        if (retain > 2) {
            for (int i = 0; i < retain; i++) {
                pattern += "0";
            }
        } else {
            pattern += "00";
        }
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(Double.parseDouble(sum));
    }
    /**
     * 为手机号码设置间隔
     */
    public static final String setIntervalsPhoneNumbers(String text) {
        String phoneNumber = "";
        int count = TextUtils.isEmpty(text) ? 0 : text.length();
        for (int i = 0; i < count; i++) {
            phoneNumber += text.substring(i, i + 1);
            if ((i - 2) % 4 == 0) {
                if (i + 1 == count) {
                    continue;
                }
                phoneNumber += " ";
            }
        }
        return phoneNumber;
    }

    /**
     * 隐藏字符串中间位置，显示长度为（3）的头与（4）尾
     */
    public static final String hideStringCenter(String text) {
        if (TextUtils.isEmpty(text)) {
            return "******";
        }
        int length = text.length();
        if (length <= 7) {
            return text;
        }
        String str = text.substring(0, 3) + "******" + text.substring(length - 4, length);
        return str;
    }
    /**
     * 根据 年、月 获取对应的月份 的 天数
     */
    public static final int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        return a.get(Calendar.DATE);
    }

    /**
     * 根据时间戳 => 时间的显示格式
     */
    public static final String getTimeFormatByTimestamp(long time, boolean isSecond) {
        // 当前时间
        long currentTime = System.currentTimeMillis();
        // 当前时间 与 传入时间的时间差
        long timeBad = currentTime - time;
        // 时间差小于一分钟
        if (timeBad < 1000 * 60) {
            return "刚刚";
        }
        // 时间差小于一小时
        if (timeBad < 1000 * 60 * 60) {
            return (int) Math.floor(timeBad / (1000 * 60)) + "分钟前";
        }
        int currentDay = Integer.parseInt(TimeUtils.with(currentTime).getDay());
        int day = Integer.parseInt(TimeUtils.with(time).getDay());
        // 时间差小于一天 并且 处于同一天
        if (timeBad < 1000 * 60 * 60 * 24 && currentDay == day) {
            return (int) Math.floor(timeBad / (1000 * 60 * 60)) + "小时前";
        }
        String reg = "HH:mm";
        if (isSecond) {
            reg += ":ss";
        }
        if (currentDay - 1 == day) {
            return "昨天  " + TimeUtils.with(time).pattern(reg).format();
        }
        reg = "yyyy-MM-dd  " + reg;
        return TimeUtils.with(time).pattern(reg).format();
    }
    /**
     * 按比例缩放图片
     */
    public static final Bitmap getScaleImageEqually(Bitmap bitmap, float scale) {
        // 获取原图片宽高
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        // 创建缩放参数
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        // 重新赋值给图片
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return bitmap;
    }
}
